package com.boding.power.common;

import net.dreamlu.controller.UeditorApiController;

import com.baidu.ueditor.UeditorConfigKit;
import com.baidu.ueditor.manager.QiniuFileManager;
import com.boding.power.controller.GoodsController;
import com.boding.power.controller.PageController;
import com.boding.power.controller.PeopleController;
import com.boding.power.controller.SysController;
import com.boding.power.controller.UserController;
import com.boding.power.intercept.SysLogInterceptor;
import com.boding.power.model.Businessinfo;
import com.boding.power.model.Comment;
import com.boding.power.model.Customer;
import com.boding.power.model.GoodsPower;
import com.boding.power.model.Permissions;
import com.boding.power.model.Photo;
import com.boding.power.model.Roles;
import com.boding.power.model.SysDict;
import com.boding.power.model.SysLog;
import com.boding.power.model.UserRoles;
import com.boding.power.model.Users;
import com.boding.power.model.Vipinfo;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.plugin.shiro.ShiroInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroPlugin;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;

/**
 * jfinal 核心配置文件
 * 
 * @author giegie
 * @description
 * 
 */
public class BodingConfig extends JFinalConfig {
	private Routes routes;

	@Override
	public void configConstant(Constants me) {
		PropKit.use("config.properties");
		me.setDevMode(PropKit.getBoolean("DevMode"));
		me.setViewType(ViewType.JSP);
		// 未授权或没有权限跳转页面
		// me.setErrorView(403, "/unauthorized.jsp");
		// 页面的基路径
		me.setBaseViewPath("/WEB-INF/pages");
	}

	@Override
	public void configRoute(Routes me) {
		me.add("/admin/user", UserController.class, "/admin/");
		me.add("/admin/people", PeopleController.class, "/admin/people");
		me.add("/admin/goods", GoodsController.class, "/admin/goods");
		me.add("/admin/sys", SysController.class, "/admin/sys");
		me.add("/ueditor/api", UeditorApiController.class);
		me.add("/front", PageController.class);// 前台页面
		this.routes = me;
	}

	@Override
	public void configPlugin(Plugins me) {
		DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"),
				PropKit.get("username"), PropKit.get("password"));
		me.add(druidPlugin);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		// 设置是否打印sql
		arp.setShowSql(PropKit.getBoolean("showSql"));
		me.add(arp);
		// ActiveRecord插件
		arp.addMapping("users", Users.class);
		arp.addMapping("roles", Roles.class);
		arp.addMapping("permissions", Permissions.class);
		arp.addMapping("photo", Photo.class);
		arp.addMapping("customer", Customer.class);
		arp.addMapping("businessinfo", Businessinfo.class);
		arp.addMapping("vipinfo", Vipinfo.class);
		arp.addMapping("customer", Customer.class);
		arp.addMapping("user_roles", "user_id", UserRoles.class);
		arp.addMapping("sys_dict", SysDict.class);
		arp.addMapping("sys_log", SysLog.class);
		arp.addMapping("goods_power", GoodsPower.class);
		arp.addMapping("comment", Comment.class);
		me.add(arp);

		// 加载Shiro插件
		ShiroPlugin shiroPlugin = new ShiroPlugin(this.routes);
		me.add(shiroPlugin);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new ShiroInterceptor());
		me.add(new SysLogInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {

	}

	public static void main(String[] args) {
		JFinal.start("src/main/webapp", 80, "/boding", 5);
	}

	@Override
	public void afterJFinalStart() {
		super.afterJFinalStart();
		String ak = PropKit.get("QINIU_ACCESS_KEY");
		String sk = PropKit.get("QINIU_SECRET_KEY");
		String bucket = PropKit.get("QINIU_bucketname");
		UeditorConfigKit.setFileManager(new QiniuFileManager(ak, sk, bucket));
	}

}
